package com.wander.rep;

import android.app.AlertDialog;
import android.app.Dialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.support.v4.app.DialogFragment;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.EditText;
import android.widget.RadioGroup;

import com.wander.rep.patient.Issue;
import com.wander.rep.server.CommonUtilities;
import com.wander.rep.server.ServerUtilities;

public class SubmitFeedbackDialog extends DialogFragment {

	private Bundle bundle;
	private int issueID;
	private Issue issue;
	private String username;

	private EditText eText;
	private RadioGroup rGroup;

	/*
	 * (non-Javadoc)
	 * @see android.support.v4.app.DialogFragment#onCreateDialog(android.os.Bundle)
	 */
	@Override
	public Dialog onCreateDialog(Bundle savedInstanceState) {

		getBundleData();

		AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());

		LayoutInflater inflater = getActivity().getLayoutInflater();

		View view = inflater.inflate(R.layout.caregiver_feedback_layout, null);

		eText = (EditText) view.findViewById(R.id.caregiver_feedback);
		rGroup = (RadioGroup) view.findViewById(R.id.radioGroup);

		builder.setView(view)
		.setPositiveButton(R.string.feedback_button_submit, new DialogInterface.OnClickListener() {

			public void onClick(DialogInterface dialog, int id) {

				String comment = eText.getText().toString();
				int outcome = 0;

				int checkedRadioButton = rGroup.getCheckedRadioButtonId();

				switch(checkedRadioButton)
				{

					case R.id.radioButtonTrue :
						outcome = 0;
						break;
					case R.id.radioButtonFalse :
						outcome = 1;
						break;

				}

				try {

					ServerUtilities.postData(issue, outcome, comment, username);

				} catch (Exception e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
		})
		.setNegativeButton(R.string.feedback_button_cancel, new DialogInterface.OnClickListener() {
			public void onClick(DialogInterface dialog, int id) {
				// User cancelled the dialog
			}
		});

		// 3. Get the AlertDialog from create()
		AlertDialog dialog = builder.create();

		return dialog;
	}

	/*
	 * Sets the Issue ID and Username as sent through Bundle Intent Data
	 */
	private void getBundleData() 
	{
		bundle = this.getArguments();
		issue = (Issue) bundle.getSerializable("issue");

		issueID = issue.getIssueID();
		username = CommonUtilities.getUsername(getActivity());
	}



}
